home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 001-025 / disk_006 / microemacs / line.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  17KB  |  464 lines

  1. /*
  2.  * The functions in this file are a general set of line management utilities.
  3.  * They are the only routines that touch the text. They also touch the buffer
  4.  * and window structures, to make sure that the necessary updating gets done.
  5.  * There are routines in this file that handle the kill buffer too. It isn't
  6.  * here for any good reason.
  7.  *
  8.  * Note that this code only updates the dot and mark values in the window list.
  9.  * Since all the code acts on the current window, the buffer that we are
  10.  * editing must be being displayed, which means that "b_nwnd" is non zero,
  11.  * which means that the dot and mark values in the buffer headers are nonsense.
  12.  */
  13.  
  14. #include        <stdio.h>
  15. #include        "ed.h"
  16.  
  17. #define NBLOCK  16                      /* Line block chunk size        */
  18. #define KBLOCK  256                     /* Kill buffer block size       */
  19.  
  20. char    *kbufp  = NULL;                 /* Kill buffer data             */
  21. int     kused   = 0;                    /* # of bytes used in KB        */
  22. int     ksize   = 0;                    /* # of bytes allocated in KB   */
  23.  
  24. /*
  25.  * This routine allocates a block of memory large enough to hold a LINE
  26.  * containing "used" characters. The block is always rounded up a bit. Return
  27.  * a pointer to the new block, or NULL if there isn't any memory left. Print a
  28.  * message in the message line if no space.
  29.  */
  30. LINE    *
  31. lalloc(used)
  32. register int    used;
  33. {
  34.         register LINE   *lp;
  35.         register int    size;
  36.  
  37.         size = (used+NBLOCK-1) & ~(NBLOCK-1);
  38.         if (size == 0)                          /* Assume that an empty */
  39.                 size = NBLOCK;                  /* line is for type-in. */
  40.         if ((lp = (LINE *) malloc(sizeof(LINE)+size)) == NULL) {
  41.                 mlwrite("Cannot allocate %d bytes", size);
  42.                 return (NULL);
  43.         }
  44.         lp->l_size = size;
  45.         lp->l_used = used;
  46.         return (lp);
  47. }
  48.  
  49. /*
  50.  * Delete line "lp". Fix all of the links that might point at it (they are
  51.  * moved to offset 0 of the next line. Unlink the line from whatever buffer it
  52.  * might be in. Release the memory. The buffers are updated too; the magic
  53.  * conditions described in the above comments don't hold here.
  54.  */
  55. lfree(lp)
  56. register LINE   *lp;
  57. {
  58.         register BUFFER *bp;
  59.         register WINDOW *wp;
  60.  
  61.         wp = wheadp;
  62.         while (wp != NULL) {
  63.                 if (wp->w_linep == lp)
  64.                         wp->w_linep = lp->l_fp;
  65.                 if (wp->w_dotp  == lp) {
  66.                         wp->w_dotp  = lp->l_fp;
  67.                         wp->w_doto  = 0;
  68.                 }
  69.                 if (wp->w_markp == lp) {
  70.                         wp->w_markp = lp->l_fp;
  71.                         wp->w_marko = 0;
  72.                 }
  73.                 wp = wp->w_wndp;
  74.         }
  75.         bp = bheadp;
  76.         while (bp != NULL) {
  77.                 if (bp->b_nwnd == 0) {
  78.                         if (bp->b_dotp  == lp) {
  79.                                 bp->b_dotp = lp->l_fp;
  80.                                 bp->b_doto = 0;
  81.                         }
  82.                         if (bp->b_markp == lp) {
  83.                                 bp->b_markp = lp->l_fp;
  84.                                 bp->b_marko = 0;
  85.                         }
  86.                 }
  87.                 bp = bp->b_bufp;
  88.         }
  89.         lp->l_bp->l_fp = lp->l_fp;
  90.         lp->l_fp->l_bp = lp->l_bp;
  91.         free((char *) lp);
  92. }
  93.  
  94. /*
  95.  * This routine gets called when a character is changed in place in the current
  96.  * buffer. It updates all of the required flags in the buffer and window
  97.  * system. The flag used is passed as an argument; if the buffer is being
  98.  * displayed in more than 1 window we change EDIT t HARD. Set MODE if the
  99.  * mode line needs to be updated (the "*" has to be set).
  100.  */
  101. lchange(flag)
  102. register int    flag;
  103. {
  104.         register WINDOW *wp;
  105.  
  106.         if (curbp->b_nwnd != 1)                 /* Ensure hard.         */
  107.                 flag = WFHARD;
  108.         if ((curbp->b_flag&BFCHG) == 0) {       /* First change, so     */
  109.                 flag |= WFMODE;                 /* update mode lines.   */
  110.                 curbp->b_flag |= BFCHG;
  111.         }
  112.         wp = wheadp;
  113.         while (wp != NULL) {
  114.                 if (wp->w_bufp == curbp)
  115.                         wp->w_flag |= flag;
  116.                 wp = wp->w_wndp;
  117.         }
  118. }
  119.  
  120. /*
  121.  * Insert "n" copies of the character "c" at the current location of dot. In
  122.  * the easy case all that happens is the text is stored in the line. In the
  123.  * hard case, the line has to be reallocated. When the window list is updated,
  124.  * take special care; I screwed it up once. You always update dot in the
  125.  * current window. You update mark, and a dot in another window, if it is
  126.  * greater than the place where you did the insert. Return TRUE if all is
  127.  * well, and FALSE on errors.
  128.  */
  129. linsert(n, c)
  130. {
  131.         register char   *cp1;
  132.         register char   *cp2;
  133.         register LINE   *lp1;
  134.         register LINE   *lp2;
  135.         register LINE   *lp3;
  136.         register int    doto;
  137.         register int    i;
  138.         register WINDOW *wp;
  139.  
  140.         lchange(WFEDIT);
  141.         lp1 = curwp->w_dotp;                    /* Current line         */
  142.         if (lp1 == curbp->b_linep) {            /* At the end: special  */
  143.                 if (curwp->w_doto != 0) {
  144.                         mlwrite("bug: linsert");
  145.                         return (FALSE);
  146.                 }
  147.                 if ((lp2=lalloc(n)) == NULL)    /* Allocate new line    */
  148.                         return (FALSE);
  149.                 lp3 = lp1->l_bp;                /* Previous line        */
  150.                 lp3->l_fp = lp2;                /* Link in              */
  151.                 lp2->l_fp = lp1;
  152.                 lp1->l_bp = lp2;
  153.                 lp2->l_bp = lp3;
  154.                 for (i=0; i<n; ++i)
  155.                         lp2->l_text[i] = c;
  156.                 curwp->w_dotp = lp2;
  157.                 curwp->w_doto = n;
  158.                 return (TRUE);
  159.         }
  160.         doto = curwp->w_doto;                   /* Save for later.      */
  161.         if (lp1->l_used+n > lp1->l_size) {      /* Hard: reallocate     */
  162.                 if ((lp2=lalloc(lp1->l_used+n)) == NULL)
  163.                         return (FALSE);
  164.                 cp1 = &lp1->l_text[0];
  165.                 cp2 = &lp2->l_text[0];
  166.                 while (cp1 != &lp1->l_text[doto])
  167.                         *cp2++ = *cp1++;
  168.                 cp2 += n;
  169.                 while (cp1 != &lp1->l_text[lp1->l_used])
  170.                         *cp2++ = *cp1++;
  171.                 lp1->l_bp->l_fp = lp2;
  172.                 lp2->l_fp = lp1->l_fp;
  173.                 lp1->l_fp->l_bp = lp2;
  174.                 lp2->l_bp = lp1->l_bp;
  175.                 free((char *) lp1);
  176.         } else {                                /* Easy: in place       */
  177.                 lp2 = lp1;                      /* Pretend new line     */
  178.                 lp2->l_used += n;
  179.                 cp2 = &lp1->l_text[lp1->l_used];
  180.                 cp1 = cp2-n;
  181.                 while (cp1 != &lp1->l_text[doto])
  182.                         *--cp2 = *--cp1;
  183.         }
  184.         for (i=0; i<n; ++i)                     /* Add the characters   */
  185.                 lp2->l_text[doto+i] = c;
  186.         wp = wheadp;                            /* Update windows       */
  187.         while (wp != NULL) {
  188.                 if (wp->w_linep == lp1)
  189.                         wp->w_linep = lp2;
  190.                 if (wp->w_dotp == lp1) {
  191.                         wp->w_dotp = lp2;
  192.                         if (wp==curwp || wp->w_doto>doto)
  193.                                 wp->w_doto += n;
  194.                 }
  195.                 if (wp->w_markp == lp1) {
  196.                         wp->w_markp = lp2;
  197.                         if (wp->w_marko > doto)
  198.                                 wp->w_marko += n;
  199.                 }
  200.                 wp = wp->w_wndp;
  201.         }
  202.         return (TRUE);
  203. }
  204.  
  205. /*
  206.  * Insert a newline into the buffer at the current location of dot in the
  207.  * current window. The funny ass-backwards way it does things is not a botch;
  208.  * it just makes the last line in the file not a special case. Return TRUE if
  209.  * everything works out and FALSE on error (memory allocation failure). The
  210.  * update of dot and mark is a bit easier then in the above case, because the
  211.  * split forces more updating.
  212.  */
  213. lnewline()
  214. {
  215.         register char   *cp1;
  216.         register char   *cp2;
  217.         register LINE   *lp1;
  218.         register LINE   *lp2;
  219.         register int    doto;
  220.         register WINDOW *wp;
  221.  
  222.         lchange(WFHARD);
  223.         lp1  = curwp->w_dotp;                   /* Get the address and  */
  224.         doto = curwp->w_doto;                   /* offset of "."        */
  225.         if ((lp2=lalloc(doto)) == NULL)         /* New first half line  */
  226.                 return (FALSE);
  227.         cp1 = &lp1->l_text[0];                  /* Shuffle text around  */
  228.         cp2 = &lp2->l_text[0];
  229.         while (cp1 != &lp1->l_text[doto])
  230.                 *cp2++ = *cp1++;
  231.         cp2 = &lp1->l_text[0];
  232.         while (cp1 != &lp1->l_text[lp1->l_used])
  233.                 *cp2++ = *cp1++;
  234.         lp1->l_used -= doto;
  235.         lp2->l_bp = lp1->l_bp;
  236.         lp1->l_bp = lp2;
  237.         lp2->l_bp->l_fp = lp2;
  238.         lp2->l_fp = lp1;
  239.         wp = wheadp;                            /* Windows              */
  240.         while (wp != NULL) {
  241.                 if (wp->w_linep == lp1)
  242.                         wp->w_linep = lp2;
  243.                 if (wp->w_dotp == lp1) {
  244.                         if (wp->w_doto < doto)
  245.                                 wp->w_dotp = lp2;
  246.                         else
  247.                                 wp->w_doto -= doto;
  248.                 }
  249.                 if (wp->w_markp == lp1) {
  250.                         if (wp->w_marko < doto)
  251.                                 wp->w_markp = lp2;
  252.                         else
  253.                                 wp->w_marko -= doto;
  254.                 }
  255.                 wp = wp->w_wndp;
  256.         }       
  257.         return (TRUE);
  258. }
  259.  
  260. /*
  261.  * This function deletes "n" bytes, starting at dot. It understands how do deal
  262.  * with end of lines, etc. It returns TRUE if all of the characters were
  263.  * deleted, and FALSE if they were not (because dot ran into the end of the
  264.  * buffer. The "kflag" is TRUE if the text should be put in the kill buffer.
  265.  */
  266. ldelete(n, kflag)
  267. {
  268.         register char   *cp1;
  269.         register char   *cp2;
  270.         register LINE   *dotp;
  271.         register int    doto;
  272.         register int    chunk;
  273.         register WINDOW *wp;
  274.  
  275.         while (n != 0) {
  276.                 dotp = curwp->w_dotp;
  277.                 doto = curwp->w_doto;
  278.                 if (dotp == curbp->b_linep)     /* Hit end of buffer.   */
  279.                         return (FALSE);
  280.                 chunk = dotp->l_used-doto;      /* Size of chunk.       */
  281.                 if (chunk > n)
  282.                         chunk = n;
  283.                 if (chunk == 0) {               /* End of line, merge.  */
  284.                         lchange(WFHARD);
  285.                         if (ldelnewline() == FALSE
  286.                         || (kflag!=FALSE && kinsert('\n')==FALSE))
  287.                                 return (FALSE);
  288.                         --n;
  289.                         continue;
  290.                 }
  291.                 lchange(WFEDIT);
  292.                 cp1 = &dotp->l_text[doto];      /* Scrunch text.        */
  293.                 cp2 = cp1 + chunk;
  294.                 if (kflag != FALSE) {           /* Kill?                */
  295.                         while (cp1 != cp2) {
  296.                                 if (kinsert(*cp1) == FALSE)
  297.                                         return (FALSE);
  298.                                 ++cp1;
  299.                         }
  300.                         cp1 = &dotp->l_text[doto];
  301.                 }
  302.                 while (cp2 != &dotp->l_text[dotp->l_used])
  303.                         *cp1++ = *cp2++;
  304.                 dotp->l_used -= chunk;
  305.                 wp = wheadp;                    /* Fix windows          */
  306.                 while (wp != NULL) {
  307.                         if (wp->w_dotp==dotp && wp->w_doto>=doto) {
  308.                                 wp->w_doto -= chunk;
  309.                                 if (wp->w_doto < doto)
  310.                                         wp->w_doto = doto;
  311.                         }       
  312.                         if (wp->w_markp==dotp && wp->w_marko>=doto) {
  313.                                 wp->w_marko -= chunk;
  314.                                 if (wp->w_marko < doto)
  315.                                         wp->w_marko = doto;
  316.                         }
  317.                         wp = wp->w_wndp;
  318.                 }
  319.                 n -= chunk;
  320.         }
  321.         return (TRUE);
  322. }
  323.  
  324. /*
  325.  * Delete a newline. Join the current line with the next line. If the next line
  326.  * is the magic header line always return TRUE; merging the last line with the
  327.  * header line can be thought of as always being a successful operation, even
  328.  * if nothing is done, and this makes the kill buffer work "right". Easy cases
  329.  * can be done by shuffling data around. Hard cases require that lines be moved
  330.  * about in memory. Return FALSE on error and TRUE if all looks ok. Called by
  331.  * "ldelete" only.
  332.  */
  333. ldelnewline()
  334. {
  335.         register char   *cp1;
  336.         register char   *cp2;
  337.         register LINE   *lp1;
  338.         register LINE   *lp2;
  339.         register LINE   *lp3;
  340.         register WINDOW *wp;
  341.  
  342.         lp1 = curwp->w_dotp;
  343.         lp2 = lp1->l_fp;
  344.         if (lp2 == curbp->b_linep) {            /* At the buffer end.   */
  345.                 if (lp1->l_used == 0)           /* Blank line.          */
  346.                         lfree(lp1);
  347.                 return (TRUE);
  348.         }
  349.         if (lp2->l_used <= lp1->l_size-lp1->l_used) {
  350.                 cp1 = &lp1->l_text[lp1->l_used];
  351.                 cp2 = &lp2->l_text[0];
  352.                 while (cp2 != &lp2->l_text[lp2->l_used])
  353.                         *cp1++ = *cp2++;
  354.                 wp = wheadp;
  355.                 while (wp != NULL) {
  356.                         if (wp->w_linep == lp2)
  357.                                 wp->w_linep = lp1;
  358.                         if (wp->w_dotp == lp2) {
  359.                                 wp->w_dotp  = lp1;
  360.                                 wp->w_doto += lp1->l_used;
  361.                         }
  362.                         if (wp->w_markp == lp2) {
  363.                                 wp->w_markp  = lp1;
  364.                                 wp->w_marko += lp1->l_used;
  365.                         }
  366.                         wp = wp->w_wndp;
  367.                 }               
  368.                 lp1->l_used += lp2->l_used;
  369.                 lp1->l_fp = lp2->l_fp;
  370.                 lp2->l_fp->l_bp = lp1;
  371.                 free((char *) lp2);
  372.                 return (TRUE);
  373.         }
  374.         if ((lp3=lalloc(lp1->l_used+lp2->l_used)) == NULL)
  375.                 return (FALSE);
  376.         cp1 = &lp1->l_text[0];
  377.         cp2 = &lp3->l_text[0];
  378.         while (cp1 != &lp1->l_text[lp1->l_used])
  379.                 *cp2++ = *cp1++;
  380.         cp1 = &lp2->l_text[0];
  381.         while (cp1 != &lp2->l_text[lp2->l_used])
  382.                 *cp2++ = *cp1++;
  383.         lp1->l_bp->l_fp = lp3;
  384.         lp3->l_fp = lp2->l_fp;
  385.         lp2->l_fp->l_bp = lp3;
  386.         lp3->l_bp = lp1->l_bp;
  387.         wp = wheadp;
  388.         while (wp != NULL) {
  389.                 if (wp->w_linep==lp1 || wp->w_linep==lp2)
  390.                         wp->w_linep = lp3;
  391.                 if (wp->w_dotp == lp1)
  392.                         wp->w_dotp  = lp3;
  393.                 else if (wp->w_dotp == lp2) {
  394.                         wp->w_dotp  = lp3;
  395.                         wp->w_doto += lp1->l_used;
  396.                 }
  397.                 if (wp->w_markp == lp1)
  398.                         wp->w_markp  = lp3;
  399.                 else if (wp->w_markp == lp2) {
  400.                         wp->w_markp  = lp3;
  401.                         wp->w_marko += lp1->l_used;
  402.                 }
  403.                 wp = wp->w_wndp;
  404.         }
  405.         free((char *) lp1);
  406.         free((char *) lp2);
  407.         return (TRUE);
  408. }
  409.  
  410. /*
  411.  * Delete all of the text saved in the kill buffer. Called by commands when a
  412.  * new kill context is being created. The kill buffer array is released, just
  413.  * in case the buffer has grown to immense size. No errors.
  414.  */
  415. kdelete()
  416. {
  417.         if (kbufp != NULL) {
  418.                 free((char *) kbufp);
  419.                 kbufp = NULL;
  420.                 kused = 0;
  421.                 ksize = 0;
  422.         }
  423. }
  424.  
  425. /*
  426.  * Insert a character to the kill buffer, enlarging the buffer if there isn't
  427.  * any room. Always grow the buffer in chunks, on the assumption that if you
  428.  * put something in the kill buffer you are going to put more stuff there too
  429.  * later. Return TRUE if all is well, and FALSE on errors.
  430.  */
  431. kinsert(c)
  432. {
  433.         register char   *nbufp;
  434.         register int    i;
  435.  
  436.         if (kused == ksize) {
  437.                 if ((nbufp=malloc(ksize+KBLOCK)) == NULL)
  438.                         return (FALSE);
  439.                 for (i=0; i<ksize; ++i)
  440.                         nbufp[i] = kbufp[i];
  441.                 if (kbufp != NULL)
  442.                         free((char *) kbufp);
  443.                 kbufp  = nbufp;
  444.                 ksize += KBLOCK;
  445.         }
  446.         kbufp[kused++] = c;
  447.         return (TRUE);
  448. }
  449.  
  450. /*
  451.  * This function gets characters from the kill buffer. If the character index
  452.  * "n" is off the end, it returns "-1". This lets the caller just scan along
  453.  * until it gets a "-1" back.
  454.  */
  455. kremove(n)
  456. {
  457.         if (n >= kused)
  458.                 return (-1);
  459.         else
  460.                 return (kbufp[n] & 0xFF);
  461. }
  462.  
  463.  
  464.